import { observable, action } from 'mobx';
import { Bubble,Tools,Modal,Toast } from 'weaver-mobile';
import { SearchAdvanced, ListViewPage } from 'weaver-mobile-page';

import * as Api from '../../api/apply';
import { PARAMS } from '../../util/index';
import QuitedCoworkStore from './QuitedCowork';

const getLabel = Tools.getLabel;
const alert = Modal.alert;

export default class ApplyStore {
  @observable quitedCoworkStore = new QuitedCoworkStore();
  @observable applyInfoSearch = new SearchAdvanced.Store({ route: '/apply', id: 'applyInfoSearch' }); //申请加入-高级搜索
  @observable applyApproveSearch = new SearchAdvanced.Store({ route: '/apply', id: 'applyApproveSearch' }); //申请审批-高级搜索
  @observable applyLogSearch = new SearchAdvanced.Store({ route: '/apply', id: 'applyLogSearch' }); //申请历史-高级搜索

  @observable applyInfoList = new ListViewPage.Store({ route: '/apply', id: 'applyInfo' }); //申请加入
  @observable applyApproveList = new ListViewPage.Store({ route: '/apply', id: 'applyApprove' }); //申请审批
  @observable applyLogList = new ListViewPage.Store({ route: '/apply', id: 'applyLog' }); //申请历史
  @observable topCounts = {};

  @observable topKey = 'apply'; //申请3个页面列表默认参数区分
  @observable visible = false; // popover显隐控制
  
  @observable showApplyCheck = false; //申请加入-批量操作
  @observable applyAdValue = ''; //高级搜索值受控
  @observable applyLoading = true;


  @observable showApplyApproveCheck = false; //申请审批-批量操作
  @observable applyApproveAdValue = ''; //高级搜索值受控
  @observable applyApproveLoading = true;
  
  @observable showApplyLogCheck = false; //申请历史-批量操作
  @observable applyLogAdValue = ''; //高级搜索值受控
	@observable applyLogLoading = true;
	@observable currentModel = {}; //当前对话框弹窗

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }
  
  @action clear = () => {
    this.topKey = 'apply';
  }
  
  @action ("申请加入列表")
  getCoworkApplyPageList = () => {
    let searchParams = this.applyInfoSearch.form.formParams || {};
    if(this.topKey === 'apply'){
       searchParams = this.applyInfoSearch.form.formParams || {};
    }else if(this.topKey === 'approval'){
       searchParams = this.applyApproveSearch.form.formParams || {}; 
    }else{
      searchParams = this.applyLogSearch.form.formParams || {};
    }

    const finalParams = {
      topKey: this.topKey,
      ...searchParams,
    }

    Api.getCoworkApplyPageList(finalParams).then((result) => {
      const dataKey = result.sessionkey;
      if (dataKey) {
        this.dataKey = dataKey;
        if(this.topKey === 'apply'){
          this.applyInfoList.getData({ dataKey }, () => this.applyLoading = false, true);// ture是否刷新用
        }else if(this.topKey === 'approval'){
          this.applyApproveList.getData({ dataKey }, () => this.applyApproveLoading = false, true);// ture是否刷新用
        }else if(this.topKey === 'applyLog'){
          this.applyLogList.getData({ dataKey }, () => this.applyLogLoading = false, true);// ture是否刷新用
        }
     
      }
    })
  }

  @action("申请加入高级搜索")
  getCoworkApplyShareCondition = () => {
      this.applyInfoSearch.getData({
        url: '/api/cowork/apply/getCoworkApplyShareCondition',
        method: 'GET',
        params: {...PARAMS},
      })
    
  }

  @action("申请加入操作")
  coworkApplayInfo = (params) => {
    let dotype = params.dotype;
    let coworkIds = params.coworkIds;
    if(dotype === undefined ){

    }else{
      if(coworkIds === ""){
        Toast.info(getLabel(390682, '请选择要加入的主题')+'!', 1);
        return;
      }
    }
    const finalParams = {
      ...params,
    };
    Api.coworkApplayInfo(finalParams).then((result) => {
      const status = result.status;
      if (status === "1") {
        this.doApplyReset();
        this.doApplyCancel();
        Bubble.success(getLabel(390683, '申请成功，请等待审批')+'!', 1);
      }
    })
  }


    // 申请加入 -- 取消
  @action doApplyCancel = () => {
    this.showApplyCheck = false;
    this.doApplyselectNone();
  }

    
  // 申请加入 -- 全选
  @action doApplySelectAll = () => this.applyInfoList.selectAll();

  // 申请加入 -- 取消全选
  @action doApplyselectNone = () => this.applyInfoList.selectNone();
  

  // 申请加入 -- 高级搜素 -- 重置
  @action doApplyReset = () => {
    this.applyInfoSearch.form.resetFields();
    this.applyAdValue = '';
    this.getCoworkApplyPageList();
  }

  // 申请加入 -- 高级搜索内外name统一
  @action updateApplyAdvance = (value) => {
    this.applyAdValue = value;
    this.applyInfoSearch.form.updateField('name', value);
  }

  @action clearInfo = () => {
    this.applyAdValue = '';
    this.applyInfoSearch.form.resetFields();
  }

  /*************************************申请审批***********************/

  @action("申请审批高级搜索")
  getCoworkApplyApproveShareCondition = () => {
      this.applyApproveSearch.getData({
        url: '/api/cowork/apply/getCoworkApplyApproveShareCondition',
        method: 'GET',
        params: {...PARAMS},
      })
    
  }

  @action("审批操作")
  doApplyApprove = (params) => {
    let dotype = params.dotype;//区分批量操作还是单独操作
    let applyIds = params.applyIds;
    if(dotype !== undefined ){
      if(applyIds === ""){
        Toast.info(getLabel(390684, '请选择要操作的主题')+'!', 1);
        return;
      }
    }
    const finalParams = {
      ...params,
    };

    this.currentModel = alert(getLabel(390849, '确认要操作吗'),'', [
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.coworkApplayApproveInfo(finalParams).then((result) => {
            const api_status = result.api_status;
            const status = result.status;
            if (api_status&&status === "1") {
              this.doApplyReset();
              this.doApplyApproveCancel();
              Bubble.success(getLabel(30700, '操作成功')+'!', 1);
            }
          })
        }
      },
    ])
  }

  @action("待审批数量")
  getCoworkApplyPageTopCounts = (params) => {
    const finalParams = {
      ...params,
    };
    Api.getCoworkApplyPageTopCounts(finalParams).then((result) => {
      const status = result.status;
      if (status === "1") {
        this.topCounts = result.topCounts;
      }
    })
  }


  // 申请审批 -- 取消
  @action doApplyApproveCancel = () => {
    this.showApplyApproveCheck = false;
    this.doApplyApproveselectNone();
  }

    
  // 申请审批 -- 全选
  @action doApplyApproveSelectAll = () => this.applyApproveList.selectAll();

  // 申请审批 -- 取消全选
  @action doApplyApproveselectNone = () => this.applyApproveList.selectNone();
  

  // 申请审批 -- 高级搜素 -- 重置
  @action doApplyApproveReset = () => {
    this.applyApproveSearch.form.resetFields();
    this.applyApproveAdValue = '';
    this.getCoworkApplyPageList();
  }

  // 申请审批 -- 高级搜索内外name统一
  @action updateApplyApproveAdvance = (value) => { 
    this.applyApproveAdValue = value;
    this.applyApproveSearch.form.updateField('name', value);
  }

  @action clearApprove = () => {
    this.applyApproveAdValue = ''
    this.applyApproveSearch.form.resetFields();
  }

  /*************************************申请历史***********************/

  
  @action("申请历史高级搜索")
  getCoworkApplyLogShareCondition = () => {
      this.applyLogSearch.getData({
        url: '/api/cowork/apply/getCoworkApplyLogShareCondition',
        method: 'GET',
        params: {...PARAMS},
      })
    
  }


  // 申请历史 -- 取消
  @action doApplyLogCancel = () => {
    this.showApplyLogCheck = false;
    this.doApplyLogselectNone();
  }

    
  // 申请历史 -- 全选
  @action doApplyLogSelectAll = () => this.applyLogList.selectAll();

  // 申请历史 -- 取消全选
  @action doApplyLogselectNone = () => this.applyLogList.selectNone();
  

  // 申请历史 -- 高级搜素 -- 重置
  @action doApplyLogReset = () => {
    this.applyLogSearch.form.resetFields();
    this.applyLogAdValue = '';
    this.getCoworkApplyPageList();
  }

  // 申请历史 -- 高级搜索内外name统一
  @action updateApplyLogAdvance = (value) => {
    this.applyLogAdValue = value;
    this.applyLogSearch.form.updateField('name', value);
  }

  // 申请历史组件卸载
  @action
  clearLog = () => {
    this.applyLogSearch.form.resetFields();
    this.applyLogAdValue = '';
	}
	
	//关闭所有弹窗
	@action closeAllModel = () => {
		this.currentModel && typeof this.currentModel.close === 'function' && this.currentModel.close();
	}
}